package com.xl.web.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xl.base.annotation.Auth;
import com.xl.base.annotation.Duplicate;
import com.xl.base.entity.Operator;
import com.xl.base.web.BaseController;
import com.xl.core.util.HtmlUtil;
import com.xl.core.util.SessionLocalUtils;
import com.xl.core.util.SessionManager;
import com.xl.core.util.json.JSONUtil;
import com.xl.web.entity.dict.DictBase;
import com.xl.web.entity.sys.SysMember;
import com.xl.web.entity.sys.SysRole;
import com.xl.web.entity.theme.MainTheme;
import com.xl.web.service.dict.DictService;
import com.xl.web.service.sys.SysMemberService;
import com.xl.web.service.sys.SysRoleService;

/**
 * 系统后台人员管理控制器
 * 
 * @author 梁华
 * 
 */
@Controller
@Scope("prototype")
@RequestMapping("/sys/member")
public class SysMemberController extends BaseController {

	// 注入系统人员管理service
	@Autowired(required = false)
	private SysMemberService<SysMember> sysMemberService;

	// 注入系统角色管理service
	@Autowired(required = false)
	private SysRoleService<SysRole> sysRoleService;

	// 注入系统角色管理service
	@Autowired(required = false)
	private DictService<DictBase> dictService;

	/**
	 * 人员列表页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.member.list")
	@RequestMapping("/list")
	public ModelAndView list(SysMember member) throws Exception {

		Map<String, Object> context = getRootMap(request);
		return forword("sys/member_list", context);
	}
	
	/**
	 * 人员列表数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.member.list")
	@ResponseBody
	@RequestMapping("/listData")
	public void listData() throws Exception {

		SysMember member = (SysMember)getJsonParams(SysMember.class);
		// 查询列表
		List<SysMember> dataList = sysMemberService.queryByList(member);
		sendListData(response, member.getPager().getRowCount(), dataList);
	}

	/**
	 * 新增人员页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.member.handle")
	@Duplicate(add = true)
	@RequestMapping("/add")
	public ModelAndView add() throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 查询角色列表
		SysRole sysRole = new SysRole();
		sysRole.getPager().setLimitDirection(false);
		List<SysRole> dataList = sysRoleService.queryByList(sysRole);
		context.put("dataList", dataList);

		return forword("sys/member_add", context);
	}

	/**
	 * 修改人员页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.member.handle")
	@Duplicate(add = true)
	@RequestMapping("/update")
	public ModelAndView update(SysMember member) throws Exception {

		Map<String, Object> context = getRootMap(request);

		// 查询本人员的详细数据
		member = sysMemberService.queryById(member);
		context.put("member", member);

		// 查询角色列表
		SysRole sysRole = new SysRole();
		sysRole.getPager().setLimitDirection(false);
		List<SysRole> dataList = sysRoleService.queryByList(sysRole);
		context.put("dataList", dataList);

		// 拥有的角色
		Map<String, String> checkRole = new HashMap<String, String>();
		List<SysRole> roleList = member.getRoleList();
		if (roleList != null && roleList.size() > 0) {
			for (SysRole role : roleList) {
				checkRole.put(role.getId(), "true");
			}
		}
		context.put("checkRole", checkRole);

		return forword("sys/member_update", context);
	}

	/**
	 * 新增人员保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.member.handle")
	@Duplicate(validate = true)
	@ResponseBody
	@RequestMapping("addSave")
	public void addSave(SysMember member, String checkedRole) throws Exception {

		// 更新或者保存人员保存
		sysMemberService.addOrUpdate(member, checkedRole);

		sendSuccessMessage(response, "添加成功");
	}
	
	/**
	 * 头像与风格保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true)
	@ResponseBody
	@RequestMapping("saveInfoSetting")
	public void saveInfoSetting(String memHeadimgUrl, MainTheme mainTheme) throws Exception {

		// 当前操作人
		Operator operator = SessionLocalUtils.getOperator();
		SysMember member = new SysMember();
		member.setId(operator.getMember().getId());
		if (StringUtils.isNotEmpty(memHeadimgUrl)) {
			member.setMemHeadimgUrl(memHeadimgUrl);
			operator.getMember().setMemHeadimgUrl(memHeadimgUrl);
		}
		if ( null !=mainTheme) {
			member.setMemMainStyle(JSONUtil.toJSONString(mainTheme));
			operator.getMember().setMemMainStyle(JSONUtil.toJSONString(mainTheme));
		}
		// 更新
		sysMemberService.updateBySelective(member);

		// 刷新缓存
		SessionManager.set(operator.getSessionId(), operator);
		sendSuccessMessage(response, "更新成功");
	}

	/**
	 * 删除人员保存
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true, permissions = "sys.member.handle")
	@ResponseBody
	@RequestMapping("deleteMember")
	public void deleteMember(SysMember member) throws Exception {

		// 逻辑删除本条数据
		sysMemberService.delete(member.getId());

		sendSuccessMessage(response, "删除成功");
	}

	/**
	 * 验证用户名是否已经存在
	 * 
	 * @param request
	 * @param responce
	 * @return
	 * @throws Exception
	 */
	@Auth(verifyLogin = true)
	@ResponseBody
	@RequestMapping("/checkMemName")
	public void checkMemName(SysMember member) throws Exception {

		// 根据用户名获取用户数据
		SysMember ckMember = sysMemberService.queryByName(member.getMemName());
		// 如果用户不存在，返回true
		if (ckMember == null || ckMember.getId() == null) {
			HtmlUtil.writerJson(response, true);
		} else if (ckMember.getId().equals(member.getId())) {
			HtmlUtil.writerJson(response, true);
		} else {
			HtmlUtil.writerJson(response, false);
		}
	}
}
